package com.fengmi.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.fengmi.dto.PageDTO;
import com.fengmi.entity.MallGoodsSpec;
import com.fengmi.entity.ViewSpecOption;
import com.fengmi.mapper.MallGoodsSpecMapper;
import com.fengmi.mapper.MallGoodsSpecOptionsMapper;
import com.fengmi.mapper.ViewSpecOptionMapper;
import com.fengmi.service.IViewSpecOptionService;
import com.fengmi.vo.PageResultVO;
import com.fengmi.vo.ResultVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 作者：陈学虎
 * 时间：2021/7/8 0008 下午 3:01
 */
@Service
@Transactional
public class ViewSpecOptionService implements IViewSpecOptionService {

    @Autowired
    private ViewSpecOptionMapper viewSpecOptionMapper;

    @Autowired
    private MallGoodsSpecMapper specMapper;

    @Autowired
    private MallGoodsSpecOptionsMapper specOptionsMapper;


    @Override
    public PageResultVO<ViewSpecOption> findPage(PageDTO<ViewSpecOption> pageDTO) {
        if (ObjectUtil.isEmpty(pageDTO)){
            return new PageResultVO<>(false,"请求参数错误");
        }

        PageHelper.startPage(pageDTO.getPage(), pageDTO.getSize());

        List<ViewSpecOption> page = viewSpecOptionMapper.findPage(pageDTO.getWhere());

        PageInfo<ViewSpecOption> pageInfo = new PageInfo<>(page);

        PageResultVO<ViewSpecOption> pageResultVO = new PageResultVO<>(true,"查询成功");

        pageResultVO.setData(pageInfo.getList());
        pageResultVO.setTotal(pageInfo.getTotal());
        pageResultVO.setPages(pageInfo.getPages());
        return pageResultVO;
    }

    @Override
    public ResultVO save(MallGoodsSpec mallGoodsSpec) {
        if (mallGoodsSpec == null){
            return new ResultVO(false,"请求参数错误");
        }


        //如果规格名称存在  则先删除再保存
        MallGoodsSpec spec = specMapper.findBySpecName(mallGoodsSpec.getSpecName());
        Integer id;
        if (spec != null){
//            specMapper.delById(mallGoodsSpec.getSpecName());
            specOptionsMapper.delBySpecId(spec.getId());
            id = spec.getId();
        }else {
            //先保存spec
            specMapper.save(mallGoodsSpec);
            id = mallGoodsSpec.getId();
        }


        //再保存option

        mallGoodsSpec.getOptions().forEach(options -> {

            options.setSpecId(id);
            int save = specOptionsMapper.save(options);
        });

        return new ResultVO(true,"保存成功");
    }

    @Override
    public ResultVO findAllSpec() {
        List<MallGoodsSpec> allSpec = specMapper.findAllSpec();
        return new ResultVO(true,"查询成功",allSpec);
    }

    @Override
    public ViewSpecOption findOne(Integer optId) {
        return viewSpecOptionMapper.findOne(optId);
    }

    @Override
    public ResultVO updateOption(ViewSpecOption viewSpecOption) {
        if (ObjectUtil.isEmpty(viewSpecOption)){
            return new ResultVO(false,"请求参数错误");
        }

        int i = specOptionsMapper.updateOption(viewSpecOption);

        return new ResultVO(i==1?true:false,i==1?"成功":"失败");
    }

    @Override
    public ResultVO delByOptId(Integer optId) {
        int i = specOptionsMapper.delByOptId(optId);
        return new ResultVO(i==1?true:false,i==1?"成功":"失败");
    }


}
